﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    public class Hr_UserLoginLogs
    {
        public Hr_UserLoginLogs()
        {
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_UserLoginLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_UserLoginLogs(");
            strSql.Append("Login_Id,Login_Ip,Login_UserCode,Login_UserName,Login_CityId,Login_City,Login_CompanyId,Login_Company,Login_DepartId,Login_DepartName,Login_Status,Login_Description,Login_Mac,Login_Time)");

            strSql.Append(" values (");
            strSql.Append("@Login_Id,@Login_Ip,@Login_UserCode,@Login_UserName,@Login_CityId,@Login_City,@Login_CompanyId,@Login_Company,@Login_DepartId,@Login_DepartName,@Login_Status,@Login_Description,@Login_Mac,@Login_Time)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Login_Id", DbType.Guid, model.Login_Id);
            db.AddInParameter(dbCommand, "Login_Ip", DbType.String, model.Login_Ip);
            db.AddInParameter(dbCommand, "Login_UserCode", DbType.String, model.Login_UserCode);
            db.AddInParameter(dbCommand, "Login_UserName", DbType.String, model.Login_UserName);
            db.AddInParameter(dbCommand, "Login_CityId", DbType.Guid, model.Login_CityId);
            db.AddInParameter(dbCommand, "Login_City", DbType.AnsiString, model.Login_City);
            db.AddInParameter(dbCommand, "Login_CompanyId", DbType.Guid, model.Login_CompanyId);
            db.AddInParameter(dbCommand, "Login_Company", DbType.AnsiString, model.Login_Company);
            db.AddInParameter(dbCommand, "Login_DepartId", DbType.Guid, model.Login_DepartId);
            db.AddInParameter(dbCommand, "Login_DepartName", DbType.AnsiString, model.Login_DepartName);
            db.AddInParameter(dbCommand, "Login_Status", DbType.AnsiString, model.Login_Status);
            db.AddInParameter(dbCommand, "Login_Description", DbType.AnsiString, model.Login_Description);
            db.AddInParameter(dbCommand, "Login_Mac", DbType.String, model.Login_Mac);
            db.AddInParameter(dbCommand, "Login_Time", DbType.DateTime, model.Login_Time);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, "Hr_UserLoginLogs");
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 返回指定UserCode的登录次数和最近登录时间
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public static DataSet GetLoginInfo(string userCode)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            string strSql = string.Format("Select count(Login_Id) from Hr_UserLoginLogs where Login_UserCode='{0}' ", userCode);
            strSql += string.Format(" select top 2 Login_Time from Hr_UserLoginLogs where Login_UserCode='{0}' order by Login_Time desc ", userCode);
            return db.ExecuteDataSet(CommandType.Text, strSql);

        }
    }
}
